package com.evildog.workspace.file.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.dependencies.mybatis.object.QueryConditionDTO;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.file.bo.BucketBO;
import com.evildog.workspace.file.bo.BucketPageBO;
import com.evildog.workspace.file.convert.BucketConvert;
import com.evildog.workspace.file.dto.BucketDTO;
import com.evildog.workspace.file.dto.BucketPageDTO;
import com.evildog.workspace.file.entity.Bucket;
import com.evildog.workspace.file.entity.BucketPageDO;
import com.evildog.workspace.file.mapper.BucketMapper;
import com.evildog.workspace.file.service.BucketService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class BucketServiceImpl extends ServiceImpl<BucketMapper, Bucket> implements BucketService {

    @Override
    public PagedResult<BucketPageBO> getPageList(PageQuery query, List<QueryConditionDTO> queryConditions, BucketPageDTO pageDTO) {
        IPage<BucketPageDO> page = this.baseMapper.getPageList(
                QueryConditionUtils.getPagination(query),
                queryConditions,
                BucketConvert.INSTANCE.convert(pageDTO));
        return BucketConvert.INSTANCE.convert(page);
    }

    @Override
    public List<BucketBO> getList(List<QueryConditionDTO> queryCondition, BucketDTO dto) {
        List<Bucket> bucketList = this.baseMapper.getList(queryCondition, BucketConvert.INSTANCE.convert(dto));
        return BucketConvert.INSTANCE.convert(bucketList);
    }

    @Override
    public BucketBO getDetailById(Long id) {
        Bucket bucket = this.getById(id);
        Assert.notNull(bucket, "bucket is null!");
        return BucketConvert.INSTANCE.convert(bucket);
    }

    @Override
    public boolean save(BucketDTO dto){
        Bucket newBucket = BucketConvert.INSTANCE.convert(dto);
        return save(newBucket);
    }

    @Override
    public boolean updateById(Long id, BucketDTO dto){
        Bucket oldBucket = getById(id);
        Assert.notNull(oldBucket, "bucket is null!");
        Bucket newBucket = BucketConvert.INSTANCE.convert(dto);
        newBucket.setId(id);
        return updateById(newBucket);
    }
}
